/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.business.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.StringUtils;
import com.rf.sys.dao.SqlAdapterMapper;
import com.rf.business.dao.CardReleaseMapper;
import com.rf.business.model.CardRelease;
import com.rf.business.model.CardReleaseExample;
import com.rf.business.model.Declaration;
import com.rf.business.service.CardReleaseService;

@Service("cardReleaseService")
public class CardReleaseServiceImpl implements CardReleaseService {
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
   	@Autowired
   	private CardReleaseMapper cardReleaseMapper;//注入dao

   	/**
	 * 根据id获取数据
	 * @param cardReleaseId
	 * @return CardRelease
	 */
   	@Override
	public CardRelease getCardReleaseByID(String id) throws Exception{
		// TODO Auto-generated method stub
		return cardReleaseMapper.selectByPrimaryKey(id);
	}
	
	/**
	 * 根据CardRelease获取数据
	 * @param CardRelease
	 * @return List<CardRelease>
	 */
	@Override
	public List<CardRelease> getCardRelease(CardRelease cardRelease) throws Exception{
		// TODO Auto-generated method stub
		CardReleaseExample example = new CardReleaseExample();
		com.rf.business.model.CardReleaseExample.Criteria criteria= example.createCriteria();
		if(cardRelease.getCarNo()!=null && !cardRelease.getCarNo().equals("")){
			criteria.andCarNoEqualTo(cardRelease.getCarNo());
		}
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		List<CardRelease> list = cardReleaseMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 获取所有数据
	 * @param 
	 * @return List<CardRelease>
	 */
	@Override
	public List<CardRelease> getCardRelease() throws Exception{
		// TODO Auto-generated method stub
		CardReleaseExample example = new CardReleaseExample();
		com.rf.business.model.CardReleaseExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		return cardReleaseMapper.selectByExample(example);
	}
   	
	/**
	 * 保存信息
	 * @param CardRelease
	 * @return
	 */
	@Override
	public void addCardRelease(CardRelease cardRelease) throws Exception{
		// TODO Auto-generated method stub
		cardReleaseMapper.insert(cardRelease);
	}

	/**
	 * 修改数据
	 * @param CardRelease
	 * @param
	 */
	@Override
	public void updateCardRelease(CardRelease cardRelease) throws Exception{
		// TODO Auto-generated method stub
		cardReleaseMapper.updateByPrimaryKeySelective(cardRelease);
	}
	
	/**
	 * 删除数据
	 * @param cardReleaseId
	 * @param
	 */
	@Override
	public void deleteCardRelease(String id) throws Exception{
		// TODO Auto-generated method stub
		cardReleaseMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 获取数据分页
	 * @param CardRelease
	 * @param pageUtils
	 * @return Map
	 */
	@Override
	public Map getCardReleasePage(CardRelease cardRelease,PageUtils pageUtils) throws Exception{
		StringBuffer sql =new StringBuffer("SELECT E.*,T.REMARK BLACK_REMARK,T.CAR_NO BLACK_CAR_NO FROM  BUS_CARD_RELEASE E,BUS_CAR_BLACKLIST T WHERE T.CAR_NO=E.CAR_NO ");
		//车牌号过滤
		if(StringUtils.isNotBlank(cardRelease.getCarNo())){
			sql.append(" AND E.CAR_NO like '%"+cardRelease.getCarNo()+"%'");
		}
		//出入境类型
		if(StringUtils.isNotBlank(cardRelease.getEntryExitType())){
			sql.append(" AND E.ENTRY_EXIT_TYPE = '"+cardRelease.getEntryExitType()+"'");
		}
		//是否已处理
		if(StringUtils.isNotBlank(cardRelease.getIsProcess())){
			sql.append(" AND E.IS_PROCESS ='"+cardRelease.getIsProcess()+"'");
		}
		//是否已接车
		if(StringUtils.isNotBlank(cardRelease.getIsMeetCar())){
			sql.append(" AND E.IS_MEET_CAR ='"+cardRelease.getIsMeetCar()+"'");
		}
		//终检亭状态
		if(StringUtils.isNotBlank(cardRelease.getFinalCheckState())){
			sql.append(" AND E.FINAL_CHECK_STATE ='"+cardRelease.getFinalCheckState()+"'");
		}
		//运管状态
		if(StringUtils.isNotBlank(cardRelease.getOperationState())){
			sql.append(" AND E.OPERATION_STATE ='"+cardRelease.getOperationState()+"'");
		}
		//南卡口状态
		if(StringUtils.isNotBlank(cardRelease.getGateCheckState())){
			sql.append(" AND E.GATE_CHECK_STATE ='"+cardRelease.getGateCheckState()+"'");
		}
		//国检状态
		if(StringUtils.isNotBlank(cardRelease.getNationCheckState())){
			sql.append(" AND E.NATION_CHECK_STATE ='"+cardRelease.getNationCheckState()+"'");
		}
		//海关状态
		if(StringUtils.isNotBlank(cardRelease.getCustomCheckState())){
			sql.append(" AND E.CUSTOM_CHECK_STATE ='"+cardRelease.getCustomCheckState()+"'");
		}
		//边检状态
		if(StringUtils.isNotBlank(cardRelease.getBorderCheckState())){
			sql.append(" AND E.BORDER_CHECK_STATE ='"+cardRelease.getBorderCheckState()+"'");
		}
		
		sql.append(" ORDER BY E.ENTRY_TIME DESC ");
		//获取第offset页，每页共limit条内容，默认查询总数count
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		//下一条执行语句，即为要被转换的语句
		List<Map<String, Object>> list=sqlAdapterMapper.selectSQL(sql.toString());
//		List<CardReleaseBean> crList = new ArrayList<CardReleaseBean>();
//		for(Map map:list){
//			CardReleaseBean cr= new CardReleaseBean();
//			cr.setId((String)map.get("ID"));
//			cr.setSerialNo((String)map.get("SERIAL_NO"));
//			cr.setCarNo((String)map.get("CAR_NO"));
//			cr.setChineseNum((BigDecimal)map.get("CHINESE_NUM"));
//			cr.setForeignNum((BigDecimal)map.get("FOREIGN_NUM"));
//			cr.setTotalNum((BigDecimal)map.get("TOTAL_NUM"));
//			cr.setNationality((String)map.get("NATIONALITY"));
//			cr.setEntryTime((Date)map.get("ENTRY_TIME"));
//			cr.setCarType((String)map.get("CAR_TYPE"));
//			cr.setEntryExitType((String)map.get("ENTRY_EXIT_TYPE"));
//			cr.setBuyInsurance((Date)map.get("BUY_INSURANCE"));
//			cr.setIsProcess((String)map.get("IS_PROCESS"));
//			cr.setFinalCheckState((String)map.get("FINAL_CHECK_STATE"));
//			cr.setFinalCheckDate((Date)map.get("FINAL_CHECK_DATE"));
//			cr.setBorderCheckState((String)map.get("BORDER_CHECK_STATE"));
//			cr.setBorderCheckDate((Date)map.get("BORDER_CHECK_DATE"));
//			cr.setNationCheckState((String)map.get("NATION_CHECK_STATE"));
//			cr.setNationCheckDate((Date)map.get("NATION_CHECK_DATE"));
//			cr.setPortCheckState((String)map.get("PORT_CHECK_STATE"));
//			cr.setPortCheckDate((Date)map.get("PORT_CHECK_DATE"));
//			cr.setCustomCheckState((String)map.get("CUSTOM_CHECK_STATE"));
//			cr.setCustomCheckDate((Date)map.get("CUSTOM_CHECK_DATE"));
//			cr.setGateCheckState((String)map.get("GATE_CHECK_STATE"));
//			cr.setGateCheckDate((Date)map.get("GATE_CHECK_DATE"));
//			cr.setOpeCheckDate((Date)map.get("OPE_CHECK_DATE"));
//			cr.setIsMeetCar((String)map.get("IS_MEET_CAR"));
//			cr.setMeetCarDate((Date)map.get("MEET_CAR_DATE"));
//			cr.setIsSendFinalCheck((String)map.get("IS_SEND_FINAL_CHECK"));
//			cr.setIsSendSouthGate((String)map.get("IS_SEND_SOUTH_GATE"));
//			cr.setOperationState((String)map.get("OPERATION_STATE"));
//			cr.setRemark((String)map.get("REMARK"));
//			cr.setLastModifyTime((Date)map.get("LAST_MODIFY_TIME"));
//			cr.setLastModifyUser((String)map.get("LAST_MODIFY_USER"));
//			cr.setCreateTime((Date)map.get("CREATE_TIME"));
//			cr.setCreateUser((String)map.get("CREATE_USER"));
//			cr.setBackCarReason((String)map.get("BACK_CAR_REASON"));
//			cr.setBackCarUser((String)map.get("BACK_CAR_USER"));
//			cr.setBackCarTime((Date)map.get("BACK_CAR_TIME"));
//			cr.setBackRemark((String)map.get("BACK_REMARK"));
//			cr.setBackDepName((String)map.get("BACK_DEP_NAME"));
//			cr.setMeetCarUser((String)map.get("MEET_CAR_USER"));
//			cr.setBLACK_CAR_NO((String)map.get("BLACK_CAR_NO"));
//			cr.setBLACK_REMARK((String)map.get("BLACK_REMARK"));
//			crList.add(cr);
//		}
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", list);
		return dateMap;
	}
	
	/**
	 * 根据id获取数据
	 * @param cardReleaseId
	 * @return CardRelease
	 */
   	@Override
	public CardRelease getCardReleaseByCarNo(String carNo) throws Exception{
		CardReleaseExample example = new CardReleaseExample();
		com.rf.business.model.CardReleaseExample.Criteria criteria= example.createCriteria();
   		if(carNo != null && !carNo.equals("")){
   			criteria.andCarNoLike("%"+carNo+"%");
   		}
   		example.setOrderByClause(" entry_time desc ");
   		//下一条执行语句，即为要被转换的语句
   		List<CardRelease> list = cardReleaseMapper.selectByExample(example);
   		CardRelease cardRelease = new CardRelease();
   		if(list.size()>0){
   			cardRelease = list.get(0);
   		}
   		return cardRelease;
	}
}

